﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Drawing.Text;
using System.Windows.Forms;

namespace Ensemble_Windows.UI
{
    /// <summary>
    /// 按钮类
    /// </summary>
    public class Button: Control
    {
        /// <summary>
        /// 宽度
        /// </summary>
        private int W;
        /// <summary>
        /// 高度
        /// </summary>
        private int H;
        private bool _Rounded = false;
        private MouseState State = MouseState.None;

        private Color _BaseColor = Helpers.BaseColor;
        private Color _TextColor = Helpers.TextColor;

        /// <summary>
        /// 基础颜色
        /// </summary>
        public Color BaseColor
        {
            get { return _BaseColor; }
            set { _BaseColor = value; }
        }

        /// <summary>
        /// 字体颜色
        /// </summary>
        public Color TextColor
        {
            get { return _TextColor; }
            set { _TextColor = value; }
        }

        /// <summary>
        /// 是否圆角
        /// </summary>
        public bool Rounded
        {
            get { return _Rounded; }
            set { _Rounded = value; }
        }

        /// <summary>
        /// 按钮按下
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseDown(MouseEventArgs e)
        {
            base.OnMouseDown(e);
            State = MouseState.Down;
            Invalidate();
        }

        /// <summary>
        /// 按钮弹起
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseUp(MouseEventArgs e)
        {
            base.OnMouseUp(e);
            State = MouseState.Over;
            Invalidate();
        }

        /// <summary>
        /// 按钮松开
        /// </summary>
        /// <param name="e"></param>
        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            State = MouseState.None;
            Invalidate();
        }

        /// <summary>
        /// 构造方法
        /// </summary>
        public Button()
        {
            SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.UserPaint | ControlStyles.ResizeRedraw
                | ControlStyles.OptimizedDoubleBuffer | ControlStyles.SupportsTransparentBackColor, true);
            DoubleBuffered = true;
            Size = new Size(106, 32);
            BackColor = Color.Transparent;
            Font = new Font("微软雅黑", 12);
            Cursor = Cursors.Hand;
        }

        /// <summary>
        /// 控件重绘
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            this.UpdateColors();
            Bitmap B = new Bitmap(Width, Height);
            Graphics G = Graphics.FromImage(B);
            W = Width - 1;
            H = Height - 1;

            GraphicsPath GP = new GraphicsPath();
            Rectangle Base = new Rectangle(0, 0, W, H);

            var _with8 = G;
            _with8.SmoothingMode = SmoothingMode.HighQuality;
            _with8.PixelOffsetMode = PixelOffsetMode.HighQuality;
            _with8.TextRenderingHint = TextRenderingHint.ClearTypeGridFit;
            _with8.Clear(BackColor);

            switch (State)
            {
                case MouseState.None:
                    if (Rounded)
                    {
                        //-- Base
                        GP = Helpers.RoundRec(Base, 6);
                        _with8.FillPath(new SolidBrush(_BaseColor), GP);

                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(_TextColor), Base, Helpers.CenterSF);
                    }
                    else
                    {
                        //-- Base
                        _with8.FillRectangle(new SolidBrush(_BaseColor), Base);

                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(_TextColor), Base, Helpers.CenterSF);
                    }
                    break;
                case MouseState.Over:
                    if (Rounded)
                    {
                        //-- Base
                        GP = Helpers.RoundRec(Base, 6);
                        _with8.FillPath(new SolidBrush(_BaseColor), GP);
                        _with8.FillPath(new SolidBrush(Color.FromArgb(20, Color.White)), GP);

                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(_TextColor), Base, Helpers.CenterSF);
                    }
                    else
                    {
                        //-- Base
                        _with8.FillRectangle(new SolidBrush(_BaseColor), Base);
                        _with8.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.White)), Base);

                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(_TextColor), Base, Helpers.CenterSF);
                    }
                    break;
                case MouseState.Down:
                    if (Rounded)
                    {
                        //-- Base
                        GP = Helpers.RoundRec(Base, 6);
                        _with8.FillPath(new SolidBrush(_BaseColor), GP);
                        _with8.FillPath(new SolidBrush(Color.FromArgb(20, Color.Black)), GP);

                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(_TextColor), Base, Helpers.CenterSF);
                    }
                    else
                    {
                        //-- Base
                        _with8.FillRectangle(new SolidBrush(_BaseColor), Base);
                        _with8.FillRectangle(new SolidBrush(Color.FromArgb(20, Color.Black)), Base);

                        //-- Text
                        _with8.DrawString(Text, Font, new SolidBrush(_TextColor), Base, Helpers.CenterSF);
                    }
                    break;
            }

            base.OnPaint(e);
            G.Dispose();
            e.Graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
            e.Graphics.DrawImageUnscaled(B, 0, 0);
            B.Dispose();
        }

        private void UpdateColors()
        {
            Colors colors = Helpers.GetColors(this);

            _BaseColor = colors.Base;
        }
    }
}
